using UnityEngine;
using System.Collections;

[AddComponentMenu("TagMachine/Entity/Trail Generator")]
public class EntityTrailGenerator : MonoBehaviour 
{
	public string TrailTag 					= "FugitiveTrail";

	public GameObject TrailPrefab 			= null;
	public float TrailDistance				= 5.0f;
	
	private Color m_TrailColor 				= Color.white;
	private NavMeshAgent m_NavAgent 		= null;
	private GameObject m_LastTrail 			= null;

	public void Setup( Color _Color, NavMeshAgent _NavAgent )
	{
		m_TrailColor 						= _Color;
		m_NavAgent 							= _NavAgent;
	}
	
	// Update is called once per frame
	void Update () 
	{
		// Add trail
		if ( GameModeClassic.Exists && GameModeClassic.Instance.CurrentWolf != null &&
			m_NavAgent.velocity.sqrMagnitude > 0.001f &&
			(m_LastTrail == null ||
			Vector3.Distance( transform.position, m_LastTrail.transform.position ) > TrailDistance) )
		{
			m_LastTrail = GameObject.Instantiate( TrailPrefab, 
                                     transform.position,
                                     Quaternion.identity ) as GameObject;
			m_LastTrail.GetComponent< EntityTrail > ().Setup( GetComponent< Fugitive >(), m_TrailColor );
		}
	}
}
